def get_words(line):
    """Return a list of the tokens in line."""
    line = line.replace("\t", " ")
    line = line.replace("\v", " ")
    line = line.replace("\r", " ")
    line = line.replace("\n", " ")
    while line.count("  "):
        line = line.replace("  ", " ")
    line = line.strip()
    return [word + " " for word in line.split(" ")]

def read_dat_file(filename):
    f = open(filename)
    allwords = []
    for line in f:
        allwords += get_words(line)
    return allwords

#read_dat_file("a05100")
def gap_allwords(allwords):
    nr = eval(allwords[0]) # number of resources 
    nj = eval(allwords[1]) # number of jobs
    n = len(allwords)
    c_begin_ind = 2
    a_begin_ind = 2 + nj*nr
    b_begin_ind = 2 + 2*nj*nr

    for i in range(2,n):
        allwords[i] = float(allwords[i])

    c = []
    a = []
    b = []
    for i in range(nr):
        row_c = allwords[c_begin_ind + i*nj :
                        c_begin_ind +(i+1)*nj]
        row_a = allwords[a_begin_ind + i*nj :
                        a_begin_ind + (i+1)*nj]
        c.append(row_c)
        a.append(row_a)

    b = allwords[b_begin_ind:]
    # print c
    # print a
    return nr, nj, c, a, b
        
def readgap(filename):
    allwords = read_dat_file(filename)
    nr, nj, c, a, b = gap_allwords(allwords)
    return nr, nj, c, a, b

